package de.dopichaj.labrador.index.own;


import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import de.dopichaj.labrador.index.WritableIndex;
import de.dopichaj.labrador.index.backend.Dictionary;
import de.dopichaj.labrador.index.backend.InvertedList;
import de.dopichaj.labrador.index.backend.Term;
import de.dopichaj.labrador.index.backend.TermFrequency;

public final class OwnWritableIndex implements WritableIndex {

    private final OwnIndex index;
    private final Dictionary dictionary;
    private final InvertedList invertedList;
    private Set<Term> knownTerms;
    
    public OwnWritableIndex(final OwnIndex index) throws IOException {
        this.index = index;
        this.invertedList = index.getInvertedList();
        this.dictionary = index.getDictionary();
    }

    public void close() throws IOException {
        invertedList.sort(dictionary);
        invertedList.close();
        final DataOutputStream dictStream = index.getDictionaryOutputStream();
        dictionary.write(dictStream);
        dictStream.close();
    }

    public void addToIndex(int fragmentID, String xPath, List<String> tokens,
        int length, int position) throws IOException {
        
        for (final TermFrequency freq : getFrequencies(tokens, fragmentID)) {
            
            invertedList.addTermFrequency(freq);
            final Term term = dictionary.getTerm(freq.getTermID());
            term.increaseElementFrequency();
            term.increaseCollectionFrequency(freq.getFrequency());
            if (!knownTerms.contains(term)) {
                term.increaseDocumentFrequency();
                knownTerms.add(term);
            }
        }
    }

    public void startNewDocument() throws IOException {
        knownTerms = new HashSet<Term>();
    }

    private List<TermFrequency> getFrequencies(final List<String> tokens,
        final int documentID) {
        
        final Map<String, Integer> freqs = new HashMap<String, Integer>();
        
        for (final String term : tokens) {
            
            if (freqs.containsKey(term)) {
                freqs.put(term, freqs.get(term) + 1);
            } else {
                freqs.put(term, 1);
            }
        }
        
        final List<TermFrequency> result = new ArrayList<TermFrequency>(freqs.size());
        for (final Map.Entry<String, Integer> entry : freqs.entrySet()) {
            final int termID = dictionary.getTerm(entry.getKey()).getID();
            final int frequency = entry.getValue();
            result.add(new TermFrequency(termID, documentID, frequency));
        }
        return result;
    }

}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/